
package com.idealighter.game.games.wknh.manager;

import com.alibaba.fastjson.JSON;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.dic.WknhBatteryDic;
import com.idealighter.game.dictionary.dic.WknhCurveDic;
import com.idealighter.game.dictionary.dic.WknhFishDic;
import com.idealighter.game.dictionary.dic.WknhRoomDic;
import com.idealighter.game.dictionary.dic.WknhScenceDic;
import com.idealighter.game.dictionary.dic.WknhStrategyDic;
import com.idealighter.game.dictionary.domain.WknhCurveDomain;
import com.idealighter.game.dictionary.domain.WknhRoomDomain;
import com.idealighter.game.dictionary.domain.WknhStrategyDomain;
import com.idealighter.game.dictionary.domwrapper.WknhFishDomWrapper;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.common.buyu.Road;
import com.idealighter.game.games.common.buyu.strategy.CircleStrategy;
import com.idealighter.game.games.common.buyu.strategy.GeneralStrategy;
import com.idealighter.game.games.common.buyu.strategy.LineStrategy;
import com.idealighter.game.games.common.buyu.strategy.PointStrategy;
import com.idealighter.game.games.common.buyu.strategy.RowStrategy;
import com.idealighter.game.games.common.buyu.strategy.ShoalStrategy;
import com.idealighter.game.games.common.buyu.strategy.SpreadStrategy;
import com.idealighter.game.games.common.buyu.strategy.Strategy;
import com.idealighter.game.games.common.buyu.strategy.StrategyType;
import com.idealighter.game.games.wknh.struct.WknhRoom;
import com.idealighter.game.games.wknh.struct.WknhSeat;
import com.idealighter.game.games.wknh.struct.WknhTable;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 悟空闹海数据管理 .
 * 
 * @date 2016年3月7日 下午8:55:42
 *
 */
@Singleton
public class WknhDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(WknhDataMgr.class);

  // 玩家的座位
  private final Map<Long, WknhSeat> playerSeats = new HashMap<>();
  // 刷鱼策略
  private final Map<Integer, Strategy> strategys = new HashMap<>();
  // 鱼的路径
  private final Map<Integer, Road> roads = new HashMap<>();
  // 游戏房间
  private final Map<Integer, WknhRoom> rooms;
  private final WknhRoomDic roomDic;
  private final WknhFishDic fishDic;
  private final WknhBatteryDic batteryDic;

  private final WknhStrategyDic strategyDic;
  private final WknhCurveDic curveDic;

  /**
   * 构造函数 .
   * 
   * @param roomDic .
   * @param fishDic .
   * @param batteryDic .
   * @param curveDic .
   * @param scenceDic .
   * @param strategyDic .
   */
  @Inject
  public WknhDataMgr(WknhRoomDic roomDic, WknhFishDic fishDic, WknhBatteryDic batteryDic,
      WknhCurveDic curveDic, WknhScenceDic scenceDic, WknhStrategyDic strategyDic) {
    this.roomDic = roomDic;
    this.fishDic = fishDic;
    this.batteryDic = batteryDic;
    this.curveDic = curveDic;
    this.strategyDic = strategyDic;
    this.rooms = createRooms();
    init();
  }

  /**
   * 创建房间 .
   * 
   * @return
   */
  private Map<Integer, WknhRoom> createRooms() {
    Map<Integer, WknhRoom> rooms = new LinkedHashMap<>();
    // 最大炮分数
    int maxScore = batteryDic.getMaxScore();
    for (WknhRoomDomain roomDom : roomDic.list()) {
      int liKui = roomDom.getLiKui();
      if (liKui > 0) {
        WknhFishDomWrapper fishDom = fishDic.get(liKui);
        rooms.put(roomDom.getId(), new WknhRoom(roomDom, fishDom.getId(), fishDom.getMinRate(),
            fishDom.getMaxRate(), maxScore));
      } else {
        rooms.put(roomDom.getId(), new WknhRoom(roomDom, 0, 0, 0, maxScore));
      }

      LOG.info("[悟空闹海]创建房间[{}][{}]", roomDom.getId(), roomDom.getName());
    }

    return rooms;
  }

  /**
   * 初始化刷鱼策略和鱼的路径 .
   */
  private void init() {
    for (WknhStrategyDomain strategyDom : strategyDic.list()) {

      StrategyType type = StrategyType.get(strategyDom.getType());
      Strategy strategy = null;

      switch (type) {
        case CIRCLE:
          strategy = JSON.parseObject(strategyDom.getData(), CircleStrategy.class);
          break;
        case SPREAD:
          strategy = JSON.parseObject(strategyDom.getData(), SpreadStrategy.class);
          break;
        case GENERAL:
          strategy = JSON.parseObject(strategyDom.getData(), GeneralStrategy.class);
          break;
        case LINE:
          strategy = JSON.parseObject(strategyDom.getData(), LineStrategy.class);
          break;
        case POINT:
          strategy = JSON.parseObject(strategyDom.getData(), PointStrategy.class);
          break;
        case ROW:
          strategy = JSON.parseObject(strategyDom.getData(), RowStrategy.class);
          break;
        case SHOAL:
          strategy = JSON.parseObject(strategyDom.getData(), ShoalStrategy.class);
          break;
        default:
          break;
      }

      strategys.put(strategyDom.getId(), strategy);
    }

    for (WknhCurveDomain curveDom : curveDic.list()) {
      Road road = new Road(curveDom);
      roads.put(road.id, road);
    }
  }

  /**
   * 获取策略 .
   * 
   * @param strategyId .
   * @return
   */
  public Strategy getStrategy(int strategyId) {

    return strategys.get(strategyId);
  }

  /**
   * 获取座位 .
   * 
   * @param playerId 玩家id.
   * @return
   */
  public WknhSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, WknhSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家seat .
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }


  /**
   * 获取房间 .
   * 
   * @param roomId .
   * @return
   */
  public WknhRoom getRoom(int roomId) {

    return rooms.get(roomId);
  }

  /**
   * 获取路径 .
   * 
   * @param roadId .
   * @return
   */
  public Road getRoad(int roadId) {

    return roads.get(roadId);
  }

  /**
   * 获取所有房间集合 .
   * 
   * @return
   */
  public Collection<WknhRoom> rooms() {

    return rooms.values();
  }

  /**
   * 获取牌桌 .
   * 
   * @param tableId .
   * @return
   */
  public WknhTable getTable(int tableId) {
    WknhRoom room = getRoom(roomId(tableId));

    if (room == null) {
      return null;
    } else {
      return room.tables.get(tableId);
    }
  }

  /**
   * 根据桌子id计算房间id .
   * 
   * @param tableId .
   * @return
   */
  public int roomId(int tableId) {

    return tableId / AbstractTable.MAX_TABLE;
  }
}
